package com.dkm.dkmproject.action;


import com.dkm.dkmproject.domain.ServicetypeTb;
import com.dkm.dkmproject.service.IServiceTbService;
import com.dkm.dkmproject.service.IServicetypeTbService;
import com.dkm.dkmproject.utils.FileTools;
import com.dkm.dkmproject.utils.Message;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务类型表 前端控制器
 * </p>
 *
 * @author 周佳佳
 * @since 2020-04-26
 */
@RestController
@RequestMapping("/dkm.dkmproject/servicetypeTb")
@Api(description = "微信小程序服务类型接口文档")
public class ServicetypeTbController {

    @Autowired
    private IServicetypeTbService servicetypeTbService;
    @Autowired
    private IServiceTbService serviceTbService;
    @Autowired
    private FileTools fileTools;

    @ApiOperation(value = "删除微信小程序服务类型的接口",notes = "删除成功返回1，删除失败返回0")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",dataType="Integer",name="syId",value = "服务类型主键")
    })
    @PostMapping("/delete")
    public Message deleteByPrimaryKey(@RequestParam(value = "syId") Integer syId) {

        Message msg = new Message();
       serviceTbService.delete(syId);
       int rows= servicetypeTbService.deleteByPrimaryKey(syId);
        if(rows>0){
            msg.setMsg("操作成功!");
            msg.setNum(1);
        }else{
            msg.setNum(0);
            msg.setMsg("操作失败");
        }

        return msg;
    }


    @ApiOperation(value = "通过syID查找微信小程序服务类型的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",dataType="Integer",name="syId",value = "服务类型主键"),
    })
    @ApiResponses({
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 403, message = "服务器拒绝请求"),
            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对"),
            @ApiResponse(code = 500, message = "后台报错"),
            @ApiResponse(code = 200, message = "返回成功")
    })
    @GetMapping("/select")
    public ServicetypeTb selectByPrimaryKey(@RequestParam(value = "syId") Integer syId) {

        if (syId!=null){
            ServicetypeTb servicetypeTb = servicetypeTbService.selectByPrimaryKey(syId);
            return servicetypeTb;
        }

        return null;
    }


    @ApiOperation(value = "查找微信小程序所有服务类型的接口")
    @GetMapping("/selectAll")
    public List<ServicetypeTb> selectByPrimaryKey() {
        return servicetypeTbService.selectAll();
    }


//    @ApiOperation(value = "增加微信小程序服务类型的接口",notes = "增加成功返回1，增加失败返回0")
//    @ApiImplicitParams({
//            @ApiImplicitParam(paramType="query",dataType="Integer",name="syId",value = "服务类型主键，增加一条数据自动生成一个id"),
//            @ApiImplicitParam(paramType="query",dataType="String",name="syName",value = "服务类型名称"),
//            @ApiImplicitParam(paramType="query",dataType="Integer",name="sort",value = "服务类型排序")
//    })
//    @GetMapping("/add")
//    public Message addServicetypeTb(ServicetypeTb servicetypeTb) {
//        Message msg = new Message();
//        int rows= servicetypeTbService.insertSelective(servicetypeTb);
//            if(rows>0){
//                msg.setMsg("操作成功!");
//                msg.setNum(1);
//            }else{
//                msg.setNum(0);
//                msg.setMsg("操作失败");
//            }
//
//        return msg;
//    }


//    @ApiOperation(value = "修改微信小程序服务类型的接口",notes = "修改成功返回1，修改失败返回0")
//    @ApiImplicitParams({
//            @ApiImplicitParam(paramType="query",dataType="Integer",name="syId",value = "服务类型主键，增加一条数据自动生成一个id"),
//            @ApiImplicitParam(paramType="query",dataType="String",name="syName",value = "服务类型名称"),
//            @ApiImplicitParam(paramType="query",dataType="Integer",name="sort",value = "服务类型排序")
//    })
//    @GetMapping("/update")
//    public Message updateServicetypeTb(ServicetypeTb servicetypeTb) {
//        Message msg = new Message();
//        if (servicetypeTb.getSyId()!=null&&!"".equals(servicetypeTb.getSyId())) {
//            int rows= servicetypeTbService.updateByPrimaryKey(servicetypeTb);
//            if(rows>0){
//                msg.setMsg("操作成功!");
//                msg.setNum(1);
//            }else{
//                msg.setNum(0);
//                msg.setMsg("操作失败");
//            }
//        }
//        return msg;
//    }
    @RequestMapping("selectAllTwo")
    @ResponseBody
    public Map<String,Object> selectAllTwo(@RequestParam("page") Integer pageNum , @RequestParam("limit") Integer pageSize,String syName){
        Map<String,Object> map=new HashMap<>();

        map.put("code",0);
        map.put("msg","");
        map.put("data",servicetypeTbService.selectAllTwo(pageNum,pageSize,syName));
        map.put("count",servicetypeTbService.selectCountOne(syName));
        return map;
    }
    @RequestMapping("addorUpdate")
    @ResponseBody
    public Message addorUpdate(ServicetypeTb servicetypeTb){
        Message message=new Message();
        if(servicetypeTb.getFileOne().getOriginalFilename()!=null &&!"".equals(servicetypeTb.getFileOne().getOriginalFilename())){
            String path = fileTools.saveFiletwo("service",servicetypeTb.getFileOne());
            servicetypeTb.setExp1(path);
        }
        //编辑操作
        if(!StringUtils.isEmpty(servicetypeTb.getSyId())){
            servicetypeTbService.updateByPrimaryKey(servicetypeTb);
        }else{
            servicetypeTbService.insertSelective(servicetypeTb);
        }
        message.setMsg("成功");
        message.setNum(1);
        return message;
    }
    @RequestMapping("findById")
    @ResponseBody
    public ServicetypeTb findById(Integer syId){
        return servicetypeTbService.selectByPrimaryKey(syId);
    }
}
